/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit_vendor_management;

/**
 *
 * @author QuangKhoi
 */
public class User {
    public String UserID;
    public String Username;
    public String Password;
    public String Name;
    public String Email;
    public String Address;
    public String RoleID;
    Database db = Database.instance;

    public User(String Username, String Password)
    {
        //this.UserID = Integer.parseInt(Database.getID("Users", "ID"));
        this.Username = Username;

        QueryResult result = db.Query("SELECT *"
                + " FROM Users WHERE Username='" + Username + "'");
        if (result.Count == 1)
        {
            UserID = result.Table.getString(0, 0);
            this.Password = result.Table.getString(0, 2);
            Name = result.Table.getString(0, 3);
            Email = result.Table.getString(0, 4);
            Address = result.Table.getString(0, 5);
            RoleID = result.Table.getString(0, 6);
        }
    }

    public User(String UserID)
    {
        this.UserID = UserID;

        QueryResult result = db.Query("SELECT * FROM Users WHERE ID='" + UserID + "'");
        if (result.Count == 1)
        {
            Username = result.Table.getString(0, 1);
            Password = result.Table.getString(0, 2);
            Name = result.Table.getString(0, 3);
            Email = result.Table.getString(0, 4);
            Address = result.Table.getString(0, 5);
            RoleID = result.Table.getString(0, 6);
        }
    }

    public Boolean Login(String UPassword)
    {
        if (UPassword.equals(Password))
            return true;
        return false;
    }

    public Boolean Save()
    {
        QueryResult result = db.Query("SELECT * FROM Users WHERE ID='" + 
                UserID + "'");
        if (result.Count == 1)
        {
            //Update
            db.Query("UPDATE Users " +
                        "SET Username='" + Username + "', Password='" + 
                        Password + "', Name='" + Name + "', Address='" + 
                        Address + "', Email='" + Email + "'" +
                        "WHERE ID='" + UserID + "'");
            return true;
        }
        else
        {
            //Add new
            db.Query("INSERT INTO Users VALUES " +
                        "('" + UserID + "', '" + Username + "', '" + Password + 
                        "', '" + Name + "', '" + Address + "', '" + Email + 
                        "', 3)");
            return true;
        }
    }

    public Boolean Delete()
    {
        db.Query("DELETE FROM Users WHERE ID = " + UserID);
        return true;
    }
}
